package com.thinkingstar.iads.projectcenter.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import sun.misc.BASE64Encoder;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;

public class FreemakerDocUtil {
    public static Configuration configure=null;
    static {configure=new Configuration(Configuration.VERSION_2_3_22);
     configure.setDefaultEncoding("utf-8");}
    public  FreemakerDocUtil(){
        
    }
    /**
     * 根据Doc模板生成word文件
     * @param dataMap 需要填入模板的数据
     * @param downloadType 文件名称
     * @param savePath 保存路径
     */
    public void createDoc(Map<String,Object> dataMap,String downloadType,String savePath,String sjnr,String sjjd){
        try {
            //加载需要装填的模板
            Template template=null;
            //设置模板装置方法和路径，FreeMarker支持多种模板装载方法。可以重servlet，classpath,数据库装载。
            //加载模板文件，放在ftl_word下
            /*String ftl_path = BusinessDirectoryFactory.getSystemBusinessPath()+"template"+UploadFileUtil.FILE_SEPARATOR+"ftl_word"+UploadFileUtil.FILE_SEPARATOR;
            if (sjnr.equals(DesignContentClassification.防护门.getName())) {
            	ftl_path += "protectivedoor"+UploadFileUtil.FILE_SEPARATOR+sjjd+UploadFileUtil.FILE_SEPARATOR;
            } else if (sjnr.equals(DesignContentClassification.客货运机械.getName())) {
            	ftl_path += "passengersgoods"+UploadFileUtil.FILE_SEPARATOR+sjjd+UploadFileUtil.FILE_SEPARATOR;
            } else if (sjnr.equals(DesignContentClassification.高铁综合维修.getName())){
            	ftl_path += "highspeedrail"+UploadFileUtil.FILE_SEPARATOR+sjjd+UploadFileUtil.FILE_SEPARATOR;;
            }*/
            String ftl_path = "";
            configure.setDirectoryForTemplateLoading(new File(ftl_path));
            /*configure.setClassForTemplateLoading(this.getClass(), "");*/
            //设置对象包装器
//            configure.setObjectWrapper(new DefaultObjectWrapper());
            //设置异常处理器
            configure.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
            //定义Template对象，注意模板类型名字与downloadType要一致
            template=configure.getTemplate(downloadType+".ftl");
            File outFile=new File(savePath);
            FileWriter out=new FileWriter(outFile);
            template.process(dataMap, out);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
    }
    
    public String getImageStr(String imgFile){
        InputStream in=null;
        byte[] data=null;
        try {
            in=new FileInputStream(imgFile);
            data=new byte[in.available()];
            in.read(data);
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        BASE64Encoder encoder=new BASE64Encoder();
        return encoder.encode(data);
    }
}
